var birthPlace,currentPlace,uw, profileUrl = path + '/mem/profile';

$(document).ready(function() {
     var sel = $('#byear');
    sel.text('');
    for (var i = 1949; i < 1995; i++) {
        $('<option/>').attr('value', i).text(i).appendTo(sel);
    }

    sel = $('#edu');
    sel.text('');
    for (var edu in  Education) {
        $('<option/>').attr('value', edu).text(Education[edu]).appendTo(sel);
    }

    if(mode == 1) { //edit
        currentPlace = new AreaSelect('area_p', 'area_cu', 'city_cu');
        birthPlace = new AreaSelect('barea_p', 'area_b', 'city_b');
        currentPlace.init(model.area);
        birthPlace.init(model.barea);

        $('#uname').val(model.uname);
        $('#byear').val(model['byear']);

        $('#edu').val(model.edu);

        $('#school_name').val(schoolName);
        $('#school').val(model.school);

        $('#years').val(model.years);

        $('input:[name=gender]:radio').each(function(){
            if(this.value == model.gender){
                this.checked = true;
            }
        });

        $('input:[name=tp]:radio').each(function(){
            if(this.value == model.tp){
                this.checked = true;
            }
        });
    } else if(mode == 0) {// new
        currentPlace = new AreaSelect('area_p', 'area_cu', 'city_cu');
        birthPlace = new AreaSelect('barea_p', 'area_b', 'city_b');
    }

    $('#univ_choose').click(function(e) {
        if (uw == null) {
            uw = new UnivWin(function(a) {
                a = $(a);
                $('#school_name').val(a.text());
                $('#school').val(a.attr('id'));
                uw.hide();
            });
        }
        uw.show();
    });

    $('#save').click(function() {
        var _self = $(this);
        var data = $('#profile').serializeJSON();
        data.area = currentPlace.value();
        data.barea = birthPlace.value();
        delete data.area_p;
        delete data.barea_p;
        delete data.school_name;

        console.log(data);
        $(this).attr('disabled', 'disabled');

        AjaxRequest(profileUrl, data, function(result) {
            if(result.status == 1) {
                InfoPanel.show('保存成功','i');
            } else {
                InfoPanel.show('保存失败，请重试','e');
            }
            _self.attr('disabled','');
        });
    })
});